﻿// <copyright file="TBatchResponseTest.cs" company="N/A">
//     Copyright (c) Nicholas Barrett and Benjamin Kasten.  All rights reserved.
//
//     This program is free software; you can redistribute it and/or modify it
//     under the terms of the GNU General Public License as published by the
//     Free Software Foundation; either version 2 of the License, or (at your
//     option) any later version.
//
//     This program is distributed in the hope that it will be useful, but
//     WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
//     Public License for more details.
//
//     You should have received a copy of the GNU General Public License along
//     with this program; if not, write to the Free Software Foundation, 
//     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// </copyright>

namespace BetterAzureTableStorage.Tests
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Text.RegularExpressions;
    using BetterAzureTableStorage;
    using Fiddler;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    /// <summary>
    /// This is a test class for TBatchResponseTest and is intended
    /// to contain all TBatchResponseTest Unit Tests
    /// </summary>
    [TestClass()]
    public class TBatchResponseTest
    {
        /// <summary>
        /// A test for TBatchResponse Constructor
        /// </summary>
        [TestMethod()]
        public void TBatchResponseConstructorTest()
        {
            using (TBatchResponse target = new TBatchResponse())
            {
            }
        }

        /// <summary>
        /// Test for ContentLength property
        /// </summary>
        [TestMethod]
        public void ContentLengthTest()
        {
            using (TBatchResponse target = new TBatchResponse())
            {
                Assert.IsNull(target.GetResponseStream());
                Assert.AreEqual<long>(0L, target.ContentLength);
                target.ContentLength = 1L;
                Assert.AreEqual<long>(0L, target.ContentLength);
            }
        }

        /// <summary>
        /// A test for Close, Content, ContentLength, and GetResponseStream
        /// </summary>
        [TestMethod()]
        public void CloseContentStreamTest()
        {
            string content = "<content goes here>";
            TBatchResponse target = new TBatchResponse();
            target.Content = content;
            Assert.AreEqual<string>(target.Content, content);
            Assert.AreEqual<long>(target.ContentLength, content.Length);
            Stream stream = target.GetResponseStream();
            Assert.IsNotNull(stream);

            byte[] expected = Encoding.UTF8.GetBytes(content);
            byte[] actual = new byte[target.ContentLength];

            stream.Read(actual, 0, (int)target.ContentLength);

            Assert.AreEqual<string>(Encoding.UTF8.GetString(expected), Encoding.UTF8.GetString(actual));

            target.Close();
        }

        /// <summary>
        /// A test for FromHttpWebResponse when the response is in an invalid state
        /// </summary>
        [TestMethod()]
        public void FromHttpWebResponseTest_InvalidStatus()
        {
            bool hasException = false;

            try
            {
                FiddlerApplication.Startup(8087, FiddlerCoreStartupFlags.Default | FiddlerCoreStartupFlags.DecryptSSL);
                FiddlerApplication.BeforeReturningError +=
                    (oSession) =>
                    {
                        if (oSession.fullUrl.ToString().Contains("InternalServerError"))
                        {
                            oSession.responseCode = (int)HttpStatusCode.InternalServerError;
                        }               
                    };

                HttpWebRequest request = WebRequest.Create("http://ipv4.fiddler:0/InternalServerError") as HttpWebRequest;

                try
                {
                    request.GetResponse();
                }
                catch (WebException ex)
                {
                    HttpWebResponse response = ex.Response as HttpWebResponse;
                    Assert.AreEqual<HttpStatusCode>(HttpStatusCode.InternalServerError, response.StatusCode);

                    try
                    {
                        IList<TBatchResponse> actual = TBatchResponse.FromHttpWebResponse(response);
                    }
                    catch (TTableStorageException)
                    {
                        hasException = true;
                    }
                }
            }
            finally
            {
                FiddlerApplication.Shutdown();
            }

            Assert.IsTrue(hasException);
        }

        /// <summary>
        /// A test for ContentID
        /// </summary>
        [TestMethod()]
        public void ContentIDTest()
        {
            using (TBatchResponse target = new TBatchResponse())
            {
                int expected = 422;
                int actual;
                target.ContentID = expected;
                actual = target.ContentID;
                Assert.AreEqual<int>(expected, actual);
            }
        }

        /// <summary>
        /// A test for ContentType
        /// </summary>
        [TestMethod()]
        public void ContentTypeTest()
        {
            using (TBatchResponse target = new TBatchResponse())
            {
                string expected = "application/http";
                string actual;
                target.ContentType = expected;
                actual = target.ContentType;
                Assert.AreEqual<string>(expected, actual);
            }
        }

        /// <summary>
        /// A test for Headers
        /// </summary>
        [TestMethod()]
        public void HeadersTest()
        {
            using (TBatchResponse target = new TBatchResponse())
            {
                Assert.IsNotNull(target.Headers);
                target.Headers.Add("KEY", "VALUE");
                Assert.AreEqual<string>("VALUE", target.Headers["KEY"]);
            }
        }

        /// <summary>
        /// A test for ResponseUri
        /// </summary>
        [TestMethod()]
        public void ResponseUriTest()
        {
            using (TBatchResponse target = new TBatchResponse())
            {
                Assert.IsNull(target.ResponseUri);
            }
        }

        /// <summary>
        /// A test for StatusCode
        /// </summary>
        [TestMethod()]
        public void StatusCodeTest()
        {
            using (TBatchResponse target = new TBatchResponse())
            {
                HttpStatusCode expected = HttpStatusCode.Ambiguous;
                HttpStatusCode actual;
                target.StatusCode = expected;
                actual = target.StatusCode;
                Assert.AreEqual<HttpStatusCode>(expected, actual);
            }
        }

        /// <summary>
        /// A test for StatusDescription
        /// </summary>
        [TestMethod()]
        public void StatusDescriptionTest()
        {
            using (TBatchResponse target = new TBatchResponse())
            {
                string expected = "Status Description Test";
                string actual;
                target.StatusDescription = expected;
                actual = target.StatusDescription;
                Assert.AreEqual<string>(expected, actual);
            }
        }
    }
}
